﻿using System;
using Grundy.Common;
using Grundy.Message;

namespace Grundy.Entity
{
    public class CustomerLicenseEntitlement
    {
        public CustomerLicenseEntitlement(Guid customerId, string customerName, LicenseEntitlement licenseEntitlement, byte[] decryptedCustomerKey)
        {
            CustomerId = customerId;
            CustomerName = customerName;
            _licenseEntitlement = licenseEntitlement;
            Key = decryptedCustomerKey;
        }

        public bool Equals(CustomerLicenseEntitlement other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return other.CustomerId.Equals(CustomerId) && other.ProductId.Equals(ProductId) && other.LicenseEntitlementId.Equals(LicenseEntitlementId);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (CustomerLicenseEntitlement)) return false;
            return Equals((CustomerLicenseEntitlement) obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int result = CustomerId.GetHashCode();
                result = (result*397) ^ ProductId.GetHashCode();
                result = (result*397) ^ LicenseEntitlementId.GetHashCode();
                return result;
            }
        }

        public static bool operator ==(CustomerLicenseEntitlement left, CustomerLicenseEntitlement right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(CustomerLicenseEntitlement left, CustomerLicenseEntitlement right)
        {
            return !Equals(left, right);
        }
        
        public Guid CustomerId { get; set; }

        public DateTime? ValidFrom { get { return _licenseEntitlement.ValidFrom; } }

        public DateTime? ValidTo { get { return _licenseEntitlement.ValidTo; } }
        
        public string CustomerName { get; set; }

        private readonly LicenseEntitlement _licenseEntitlement;

        public License License { get { return _licenseEntitlement.License; } }

        public Guid ProductId { get { return _licenseEntitlement.Product.Id; } }

        public string ProductName { get { return _licenseEntitlement.Product.Name; } }

        public Guid LicenseEntitlementId { get { return _licenseEntitlement.Id; } }

        public IdentifierCollection NodeIdentity { get { return _licenseEntitlement.Node.Identifiers; } }

        public byte[] Key { get; private set; }

        public FeatureIdentificationKey ToFeatureIdentificationKey()
        {
            return new FeatureIdentificationKey
                       {
                           CustomerId = CustomerId,
                           CustomerName = CustomerName,
                           ProductId = ProductId,
                           ProductName = ProductName,
                           LicenseEntitlementId = LicenseEntitlementId,
                           LicenseId = License.Id,
                           LicenseName = License.Name,
                           FeatureId = License.Feature.Id,
                           FeatureName = License.Feature.Name,
                       };
        }
    }
}